<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.input.php
 * Type:     function
 * Name:     input
 * Purpose:  outputs html for a form input
 * -------------------------------------------------------------
 */
function smarty_function_input($params, &$smarty) {
    $object = $smarty->_tpl_vars[$params['object']];
    $mapping = $params['object'] . '.' . $params['name'];
	$readonly = @$params['readonly'] == true;

	if($readonly === false) {		
	    if(@$params['type'] === 'date') {
	        $html .= '<script type="text/javascript">$(function() { $("#' . $mapping . '").datePicker({});$("#' . $mapping . '").dpSetStartDate("01/01/1900");});</script>';
	    }
	}
	    
    if(@$params['label'] != '') {
        $html .= '<label>';
        $html .= $params['required'] == true ? '<em>*</em>' : '';
        $html .= $params['label'] . ':</label><br />';
    }
	
	if($readonly === false) {
	    $html .= '<input type="' . (@$params['type'] === 'password' ? 'password' : 'text') . '" name="' . $mapping . '" id="' . $mapping . '"';
	    $html .= ' value="';
	}
	    
	if($readonly === true) {
		$html .= '<span class="data">';
	}
	
    if(@$params['type'] === 'date') {
        if($object->$params['name'] != '') {
            $html .= date('m/d/Y', strtotime($object->$params['name']));
        }
    }else if(@$params['type'] !== 'password') {
        $html .= stripslashes($object->$params['name']);
    }
	
	if($readonly === true) {
		$html .= '</span>';
	}
	    
	if($readonly === false) {
		$error = Adroit::$alerts->getAlertById($mapping, AdroitAlertHandler::$TYPE_ERROR);
		
	    $html .= '" class="textbox' . ($error != null ? ' input-error' : '') . '" ';
	    
	    if(@$params['type'] === 'date') {
	        $html .= 'style="float:left;width:100px;" ';
	    }
	    
	    $html .= '/>';
	
		
		if($error != null) {
			$html .= '<span class="input-error-message">' . $error->message . '</span>';
		}
	}
    
    return $html;
}
?>